package com.czh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czh.constants.SystemConstants;
import com.czh.domain.ResponseResult;
import com.czh.domain.dto.QueryLinkDto;
import com.czh.domain.vo.LinkPageVo;
import com.czh.domain.vo.LinkShowVo;
import com.czh.mapper.LinkMapper;
import com.czh.domain.entity.Link;
import com.czh.service.LinkService;
import com.czh.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
/**
 * 友链(Link)表服务实现类
 *
 * @author makejava
 * @since 2024-03-20 22:54:48
 */
@Service("linkService")
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {

    @Override
    public ResponseResult<List<LinkShowVo>> getAllLink() {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.eq(Link::getStatus, SystemConstants.LINK_STATUS_NORMAL);
        List<Link> linkList = list(linkLambdaQueryWrapper);
        List<LinkShowVo> linkShowVos = BeanCopyUtils.copyBeanList(linkList, LinkShowVo.class);
        return ResponseResult.okResult(linkShowVos);
    }
    /*
      admin
     */
    @Override
    public ResponseResult<LinkPageVo> pageLink(Integer pageNum, Integer pageSize, QueryLinkDto queryLinkDto) {
        LambdaQueryWrapper<Link> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        linkLambdaQueryWrapper.like(StringUtils.hasText(queryLinkDto.getName()), Link::getName, queryLinkDto.getName()).
                eq(StringUtils.hasText(queryLinkDto.getStatus()), Link::getStatus, queryLinkDto.getStatus());

        List<LinkShowVo> linkShowVos = BeanCopyUtils.copyBeanList(list(linkLambdaQueryWrapper), LinkShowVo.class);
        return ResponseResult.okResult(new LinkPageVo(linkShowVos, (long) linkShowVos.size()));
    }

    @Override
    public ResponseResult<Object> addLink(Link link) {
        save(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<LinkShowVo> getLinkById(Long id) {
        return ResponseResult.okResult(BeanCopyUtils.copyBean(getById(id), LinkShowVo.class));
    }

    @Override
    public ResponseResult<Object> updateLink(Link link) {
        updateById(link);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult<Object> deleteLink(Long id) {
        removeById(id);
        return ResponseResult.okResult();
    }
}

